﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Galaktika.BI.Runtime;
using Galaktika.BI.Runtime.Services;
using Galaktika.BI.Silverlight.ClientApp.Services;

namespace Galaktika.BI.Silverlight.ClientApp
{
	public partial class FormContainer : UserControl, IFormContainer
	{
		
		private string m_formId;
		public FormContainer(string formId)
		{
			InitializeComponent();
			App.CurrentApp.AddService(typeof(IUserTaskManager), new UserTaskManager(null, txtStatus));
			m_formId = formId;
		}

        //private FormView fv;
        //internal FormView FormView
        //{
        //    get { return fv;}
        //}
		#region IFormContainer Members

		public UserControl RootControl
		{
			get { return this; }
		}

		public void Initialize(List<Galaktika.BI.Extensibility.Dom.Workstation.BrickWorkstation> workstations)
		{
			this.Navigator.NavigateTo(m_formId,true,true);
		}

		private StubFormNavigatorAppManager m_Navigator;
		public IFormNavigator Navigator
		{
			get
			{
				if (m_Navigator == null)
				{
					m_Navigator = new StubFormNavigatorAppManager(this);
				}
				return m_Navigator;
			}
		}

		public Galaktika.BI.Runtime.Services.IAppManager AppManager
		{
			get
			{
				if (m_Navigator == null)
				{
					m_Navigator = new StubFormNavigatorAppManager(this);
				}
				return m_Navigator;
			}
		}

        public ITabPageFormResolver TabPageFormResolver
        {
            get
            {
                if (m_Navigator == null)
                {
                    m_Navigator = new StubFormNavigatorAppManager(this);
                }
                return m_Navigator;
            }
        }

		class StubFormNavigatorAppManager : Galaktika.BI.Runtime.Services.IFormNavigator, Galaktika.BI.Runtime.Services.IAppManager, ITabPageFormResolver
		{
			internal StubFormNavigatorAppManager(FormContainer fc)
			{
				m_fc = fc;
			}

			private FormView fv;
			private FormContainer m_fc;
            private bool NavigateTo(string formId, bool openInNewWindow, bool logToHistory, IDictionary<string, string> args)
            {
                try
                {
                    fv = new FormView();
                    fv.Id = formId;
                    IBrickContainer container = ClientResources.GetContainer(formId);
                    if (container == null)
                    {
                        m_fc.pnlContent.Content = new ExceptionControl() { Text = string.Format("Invalid form id: {0}", formId) };
                    }
                    else
                    {
                        fv.Initialize(container);
                        m_fc.pnlContent.Content = fv;
                        IBindingContext context = App.CurrentApp.Binder.GetContext(fv);
                        if (context == null)
                        {
                            throw new UnableToResolveBindingContextException(fv);
                        }
                        App.CurrentApp.Binder.RefreshImmediate(context);
                    }
                }
                catch (Exception exc)
                {
                    m_fc.pnlContent.Content = new ExceptionControl() { Text = exc.ToString() };
                }
                return true;
            }

			public bool NavigateTo(string formId, bool openInNewWindow, bool logToHistory)
			{
                return this.NavigateTo(formId, openInNewWindow, logToHistory, null);
			}

            public bool NavigateTo(IDictionary<string, string> args)
            {
                string openInNewWindow = args["openInNewWindow"];
                bool isNewWindow;
                bool.TryParse(openInNewWindow, out isNewWindow);
                return this.NavigateTo(args["formId"], isNewWindow, true, args);
            }

			public bool GoBack()
			{
				return true;
			}

			public bool GoForward()
			{
				return true;
			}

			public void PushState()
			{
				
			}

			public void PopState()
			{
				
			}

			public object CurrentForm
			{
				get { return fv; }
			}

			public event EventHandler CurrentFormChanged;
            protected void OnCurrentFormChanged()
            {
                if (this.CurrentFormChanged != null)
                {
                    this.CurrentFormChanged(this, EventArgs.Empty);
                }
            }

			public event EventHandler SaveCurrentState;
            protected void OnSaveCurrentState()
            {
                if (this.SaveCurrentState != null)
                {
                    this.SaveCurrentState(this, EventArgs.Empty);
                }
            }

            public event EventHandler LoadCurrentState;
            protected void OnLoadCurrentState()
            {
                if (this.LoadCurrentState != null)
                {
                    this.LoadCurrentState(this, EventArgs.Empty);
                }
            }

            public void UpdateCurrentContext()
			{
				
			}

			public void UpdateNavigationBar()
			{
				
			}

			public void ResetForm()
			{
				
			}

			public object Owner
			{
				get { return m_fc; }
			}

			public string GetAppTitle()
			{
				return string.Format("{0} - {1}", ClientResources.GetGlobalValue("_$ProductName"), fv.Container.Title);//, Localization.App_Title);
			}

			public Assembly ClientAssembly
			{
				get { return ClientResources.ClientAssembly; }
			}

			public void AttachHelp(IBrickContainer container, object root)
			{
			}

            #region ITabPageFormResolver Members

            public FormView GetForm(Galaktika.BI.Silverlight.ClientApp.Items.TabPage tabPage)
            {
                return fv;
            }

            #endregion
        }
        
		#endregion
	}
}
